// -----------------------------------------------------------------------
// <copyright file="UserEN.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.ENTITIES
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    
    /// <summary>
    /// Clase User
    /// </summary>
    public class UserEN : EntityEN
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="UserEN" /> class
        /// </summary>
        public UserEN()
        {
            this.UserId = 0;
            this.AdministrativeUnitsId = 0;
            this.UserName = string.Empty;
            this.FullName = string.Empty;
            this.Sex = false;
            this.BirthDate = null;
            this.Email = string.Empty;
            this.Sms = string.Empty;
            this.Phone = string.Empty;
            this.Address = string.Empty;
            this.CreateDate = null;
            this.Send = string.Empty;
        }

        /// <summary>
        /// Gets or sets UserId
        /// </summary>
        public int UserId { get; set; }

        /// <summary>
        /// Gets or sets AdministrativeUnitsId
        /// </summary>
        public int AdministrativeUnitsId { get; set; }

        /// <summary>
        /// Gets or sets UserName
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// Gets or sets FullName
        /// </summary>
        public string FullName { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether Sex
        /// </summary>
        public bool Sex { get; set; }

        /// <summary>
        /// Gets or sets BirthDate
        /// </summary>
        public DateTime? BirthDate { get; set; }

        /// <summary>
        /// Gets or sets Email
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// Gets or sets Sms
        /// </summary>
        public string Sms { get; set; }

        /// <summary>
        /// Gets or sets Phone
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// Gets or sets Address
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// Gets or sets CreateDate
        /// </summary>
        public DateTime? CreateDate { get; set; }

        /// <summary>
        /// Gets or sets Send
        /// </summary>
        public string Send { get; set; }

        /// <summary>
        /// Gets AdministrativeUnit
        /// </summary>
        public AdministrativeUnitEN AdministrativeUnit
        {
            get
            {
                try
                {
                    AdministrativeUnitEN administrativeUnit = null;
                    SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneAdministrativeUnitGetById, this.AdministrativeUnitsId);

                    while (reader.Read())
                    {
                        administrativeUnit = new AdministrativeUnitEN();

                        if (!reader.IsDBNull(0))
                        {
                            administrativeUnit.AdministrativeUnitsId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            administrativeUnit.Code = reader.GetString(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            administrativeUnit.Name = reader.GetString(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            administrativeUnit.Parent = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            administrativeUnit.Order = reader.GetInt32(4);
                        }
                    }

                    reader.Close();
                    reader.Dispose();

                    return administrativeUnit;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }

        /// <summary>
        /// Gets UserDepartments
        /// </summary>
        public IList<UserDepartmentEN> UserDepartments
        {
            get
            {
                try
                {
                    IList<UserDepartmentEN> lstUserDepartment = new List<UserDepartmentEN>();
                    var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneUserDepartmentGetByUserId, this.UserId);
                    while (reader.Read())
                    {
                        UserDepartmentEN userDepartment = new UserDepartmentEN();
                        if (!reader.IsDBNull(0))
                        {
                            userDepartment.UserDepartmentId = reader.GetInt32(0);
                        }

                        if (!reader.IsDBNull(1))
                        {
                            userDepartment.UserId = reader.GetInt32(1);
                        }

                        if (!reader.IsDBNull(2))
                        {
                            userDepartment.DepartmentId = reader.GetInt32(2);
                        }

                        if (!reader.IsDBNull(3))
                        {
                            userDepartment.PositionsId = reader.GetInt32(3);
                        }

                        if (!reader.IsDBNull(4))
                        {
                            userDepartment.Active = reader.GetBoolean(4);
                        }

                        if (!reader.IsDBNull(5))
                        {
                            userDepartment.Default = reader.GetBoolean(5);
                        }

                        lstUserDepartment.Add(userDepartment);
                    }

                    reader.Close();
                    reader.Dispose();
                    return lstUserDepartment;
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
        }
    }
}
